package com.ch.贪心;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Stack;

/**
 * @author chenhong
 * @version 1.0.0
 * @ClassName N763.java
 * @Description TODO
 * @createTime 2021年05月24日 21:53:00
 */
public class N763 {
    public static void main(String[] args) {
        Solution763 s = new Solution763();
        s.partitionLabels("abcdealk");
    }
}
class Solution763 {
    public List<Integer> partitionLabels(String s) {
        List<Integer> ans = new ArrayList<>();
        int []count = new int[26];
        for(char c:s.toCharArray()){
            count[c-97]++;
        }
        //已经计入的字母的种类
        HashSet<Character> hashSet = new HashSet<>();
        //已经计入的字符串长度
        int num = 0;

        for(char c : s.toCharArray()){
            num++;
            if(!hashSet.contains(c)){
                hashSet.add(c);
            }
            if(--count[c-97] == 0){
                hashSet.remove(c);
                if(hashSet.isEmpty()){
                    ans.add(num);
                    num = 0;
                }
            }
        }

        return ans;
    }
}


//

class Solution763_2 {
    public List<Integer> partitionLabels(String s) {
        int[] last = new int[26];
        int length = s.length();
        for (int i = 0; i < length; i++) {
            last[s.charAt(i) - 'a'] = i;
        }
        List<Integer> partition = new ArrayList<Integer>();
        int start = 0, end = 0;
        for (int i = 0; i < length; i++) {
            end = Math.max(end, last[s.charAt(i) - 'a']);
            if (i == end) {
                partition.add(end - start + 1);
                start = end + 1;
            }
        }
        return partition;
    }
}